package com.ecoolex.ying.space.api.graphql;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.alibaba.fastjson.JSON;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoods;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsCategory;
import com.ecoolex.ying.space.ws.EstateProjectActivityService;
import com.google.common.collect.Lists;

@GraphQL2Module(type = GraphQL2Module.Type.User, description = "地产项目活动商品分类查询")
public class EstateProjectActivityGoodsCategoryGraphql {

	@Reference
	private EstateProjectActivityService service;
	
	@Autowired
    private StorageService storageService;
	
	@TypeField(type = EstateProjectActivityGoods.class,field = "activityCoverImage",description = "活动商品封面图片")
	public StorageInfo activityCoverImage(@Source EstateProjectActivityGoods source) {
		return storageService.get(source.getActivityCoverImage());
	}
	
	@TypeField(type = EstateProjectActivityGoods.class, field = "images", description = "地产项目活动商品轮播图片")
	public List<StorageInfo> images(@Source EstateProjectActivityGoods source){
		if(Check.notNullOrTrimEmpty(source.getImages())) {
			return JSON.parseArray(source.getImages(), String.class).stream()
				.map(storageService::get)
				.filter(Check::notNull)
				.collect(Collectors.toList());
		}
		return Lists.newArrayList();
	}
	
	@TypeField(type = EstateProjectActivityGoodsCategory.class,field = "activityGoodsList",description = "活动商品列表")
	public Pagination<EstateProjectActivityGoods> activityGoodsList(@Source EstateProjectActivityGoodsCategory source,@NonNull Page page){
		return service.queryEstateProjectActivityGoodsByCategoryId(source.getActivityCategoryId(), page);
	}
	
	@QueryField(field = "estateProjectActivityGoods",description = "地产项目活动商品")
	public EstateProjectActivityGoods getEstateProjectActivityGoods(@NonNull Integer goosId) {
		return service.getEstateProjectActivityGoods(goosId);
	}
	
	@QueryField(field = "estateProjectActivityGoodsCategoryList",description = "地产项目活动商品分类列表")
	public Pagination<EstateProjectActivityGoodsCategory> query(Integer parentCategoryId,@NonNull Page page,ConsumerMemberAuthority user){
		user.assertLogin();
		return service.queryEstateProjectActivityGoodsCategoryOnline(parentCategoryId, page);
	}
	
	@QueryField(field = "estateProjectActivityGoodsCategory",description = "地产项目活动商品分类")
	public EstateProjectActivityGoodsCategory get(Integer activityCategoryId,ConsumerMemberAuthority user){
		user.assertLogin();
		return service.getEstateProjectActivityGoodsCategoryOnline(activityCategoryId);
	}
}
